﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XChipTool.Enties;
using XChip_Common;

namespace XChipTool
{
    public partial class XChip_BinChoice : DevExpress.XtraEditors.XtraForm
    {
        private int isPress_key;
        private List<BinChoiceData> listC=new List<BinChoiceData>();
        private bool isSaveMask =false;//保存标记
        public XChip_BinChoice()
        {
            InitializeComponent();        
        }

        public XChip_BinChoice(int _isPress_key,List<BinChoiceData> _list) {
            isPress_key = _isPress_key;
            listC = _list;
            //listC.ForEach(c => c.isDel = false);
            InitializeComponent();
        }

        
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listC.Count; i++)
            {
                listC[i].Sort = i;
                listC[i].isTick = false;
                listC[i].isDel = false;
            }
            this.gridControl1.DataSource = listC.OrderBy(c=>c.Sort);
            this.gridView1.RefreshData();
        }

      
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton4_Click(object sender, EventArgs e)
        {
           
            var delList = listC.Where(c => c.isTick == true);
            listC.ForEach(c => c.isDel = false);
            
            listC.ForEach(c => c.isDel = c.isTick);
            int i = 0;
            var tempListC = listC.OrderBy(c => c.Sort);
            foreach (var item in tempListC.Where(c => c.isDel == false))
            {
                item.Sort = i;
                i++;
            }
            foreach (var item in tempListC.Where(c => c.isDel == true))
            {
                item.Sort = i;
                i++;
            }
            this.gridControl1.DataSource = listC.Where(c=>c.isDel == false).OrderBy(c => c.Sort);
            this.gridView1.RefreshData();
        }

        private void XChip_BinChoice_Load(object sender, EventArgs e)
        {
            switch (isPress_key)
            {
                case 0:
                    {
                        
                        this.Text = "按键轮询";
                        int num = Convert.ToInt16(ParaHelp.Bin_Press_Key.Find(p => p.PKey == "0x0b20").PValue, 16);
                        if (num == 0)
                        {
                           
                            return;
                        }
                        if (Convert.ToInt16(ParaHelp.Bin_Press_Key.Find(p => p.PKey == "0x0b38").PValue, 16) == 0x4)
                        {
                            cmb_Polling_Pin_Select.SelectedIndex = 1;
                        }
                        else {
                            cmb_Polling_Pin_Select.SelectedIndex = Convert.ToInt16(ParaHelp.Bin_Press_Key.Find(p => p.PKey == "0x0b38").PValue, 16);
                        }
                        
                        foreach (var item in listC)
                        {
                            item.isTick = true;//全部设置成不显示
                            item.isDel = true;
                        }
                        for (int i = 0x0b21; i < 0x0b21 + num; i++)
                        {
                            string a =$"0x{i.ToString("x4")}";
                            listC.First(c => c.tag == Convert.ToInt32(ParaHelp.Bin_Press_Key.First(t => t.PKey == $"0x{i.ToString("x4")}").PValue.Replace("0x", ""),16)).Sort = i;
                            listC.First(c => c.tag == Convert.ToInt32(ParaHelp.Bin_Press_Key.First(t => t.PKey == $"0x{i.ToString("x4")}").PValue.Replace("0x", ""), 16)).isTick = false;
                            listC.First(c => c.tag == Convert.ToInt32(ParaHelp.Bin_Press_Key.First(t => t.PKey == $"0x{i.ToString("x4")}").PValue.Replace("0x", ""), 16)).isDel = false;
                        }
                        gridControl1.DataSource = listC.Where(c => c.isDel == false).OrderBy(c => c.Sort);
                        gridView1.RefreshData();
                    }
                    break;
                case 1:
                    {
                        
                        this.Text = "ADC轮询";
                        if (ParaHelp.Bin_ADC[11].PValue.Contains("00")&& ParaHelp.Bin_ADC[12].PValue.Contains("00")&& ParaHelp.Bin_ADC[13].PValue.Contains("00")&& ParaHelp.Bin_ADC[14].PValue.Contains("00"))
                        {
                            return;
                        }

                        foreach (var item in listC)
                        {
                            item.isTick = true;//全部设置成不显示
                            item.isDel = true;
                        }
                        uint temp0 = Convert.ToUInt16(ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c17").PValue, 16);
                        uint temp1 = Convert.ToUInt16(ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c18").PValue, 16);
                        uint temp2 = Convert.ToUInt16(ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c19").PValue, 16);
                        uint temp3 = Convert.ToUInt16(ParaHelp.Bin_ADC.First(t => t.PKey == "0x0c1a").PValue, 16);
                        listC.First(c => c.tag == temp0).Sort = 0;
                        listC.First(c => c.tag == temp0).isTick = false;
                        listC.First(c => c.tag == temp0).isDel = false;

                        listC.First(c => c.tag == temp1).Sort = 1;
                        listC.First(c => c.tag == temp1).isTick = false;
                        listC.First(c => c.tag == temp1).isDel = false;

                        listC.First(c => c.tag == temp2).Sort = 2;
                        listC.First(c => c.tag == temp2).isTick = false;
                        listC.First(c => c.tag == temp2).isDel = false;

                        listC.First(c => c.tag == temp3).Sort = 3;
                        listC.First(c => c.tag == temp3).isTick = false;
                        listC.First(c => c.tag == temp3).isDel = false;

                        gridControl1.DataSource = listC.Where(c => c.isDel == false).OrderBy(c => c.Sort);
                        gridView1.RefreshData();
                    }
                    break;
                default:
                    this.Text = "轮询";
                    break;
            }
        }
        /// <summary>
        /// 上
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            var row = gridView1.GetSelectedRows();
            var Id =gridView1.GetRowCellValue(row[0], "BinName").ToString();
            int focused = -1;
            if (listC.First(c => c.BinName == Id).Sort == 0)
            {
                return;
            }
            if (listC.First(c => c.BinName == Id).Sort - 1 >= 0)
            {
                listC.First(c => c.Sort == listC.First(t => t.BinName == Id).Sort - 1).Sort += 1;
                focused = (listC.First(c => c.BinName == Id).Sort -= 1);

            }
            


            gridControl1.DataSource = listC.Where(c => c.isDel == false).OrderBy(c => c.Sort);
            gridView1.RefreshData();
            //gridView1.SelectRow(focused);
            gridView1.FocusedRowHandle = focused;

            gridView1.SetMasterRowExpanded(focused, true);
            gridView1.Focus();
        }
        /// <summary>
        /// 下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            var row = gridView1.GetSelectedRows();
            var Id = gridView1.GetRowCellValue(row[0], "BinName").ToString();
            if (listC.First(c => c.BinName == Id).Sort == listC.Where(c => c.isDel == false).Max(c => c.Sort))
            {
                return;
            }
            int focused = -1;
            if (listC.First(c => c.BinName == Id).Sort + 1 <= listC.Max(c => c.Sort))
            {
                listC.First(c => c.Sort == listC.First(t => t.BinName == Id).Sort + 1).Sort -= 1;
                focused = listC.First(c => c.BinName == Id).Sort += 1;
            }
            else {
                focused = listC.Max(c => c.Sort);
            }


            gridControl1.DataSource = listC.Where(c => c.isDel == false).OrderBy(c => c.Sort);
            gridView1.RefreshData();
            //gridView1.SelectRow(focused);
            gridView1.FocusedRowHandle = focused;

            gridView1.SetMasterRowExpanded(focused, true);
            gridView1.Focus();
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton5_Click(object sender, EventArgs e)
        {
            isSaveMask = SavePollingSetting();
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns>是否成功</returns>
        private bool SavePollingSetting()
        {
            try
            {
                if (listC.Count(c => c.isDel == false) == 0)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("未加载!");
                    return false;
                }
                if (isPress_key == 0 && listC.Count(c => c.isDel == false) > 16)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("按键轮询只支持16套");
                    return false;
                }
                if (isPress_key == 1 && listC.Count(c => c.isDel == false) > 4)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("ADC轮询只支持4套");
                    return false;
                }
                if (isPress_key == 0)
                {
                    var list = listC.Where(c => c.isDel == false).OrderBy(c => c.Sort).ToList();
                    ParaHelp.Bin_Press_Key.Find(p => p.PKey == "0x0b1e").PValue = $"0x01";
                    var combtext = cmb_Polling_Pin_Select.Properties.Items[cmb_Polling_Pin_Select.SelectedIndex].ToString();
                    if (combtext.Contains("ADC"))
                    {
                        ParaHelp.Bin_Press_Key.Find(p => p.PKey == "0x0b38").PValue = $"0x04";
                    }
                    else {
                        ParaHelp.Bin_Press_Key.Find(p => p.PKey == "0x0b38").PValue = $"0x{cmb_Polling_Pin_Select.SelectedIndex.ToString("x2").PadLeft(2, '0')}";
                    }
                       
                    ParaHelp.Bin_Press_Key.Find(p => p.PKey == "0x0b20").PValue = $"0x{list.Count().ToString("x2").PadLeft(2, '0')}";
                    
                    for (int i = 0x0b21,j =0; i < 0x0b21 + list.Count; i++,j++)
                    {
                        string a = $"0x{i.ToString("x4")}";
                        ParaHelp.Bin_Press_Key.Find(p => p.PKey == a).PValue = $"0x{(list.Count > 0 ? list[j].tag.ToString("x2").PadLeft(2, '0') : "00")}";                       
                    }
                    

                }
                if (isPress_key == 1)
                {
                    var list = listC.Where(c => c.isDel == false).OrderBy(c => c.Sort).ToList();

                    ParaHelp.Bin_ADC[11].PValue = $"0x{(list.Count > 0 ? list[0].tag.ToString("x2").PadLeft(2, '0') : "00")}";
                    ParaHelp.Bin_ADC[12].PValue = $"0x{(list.Count > 1 ? list[1].tag.ToString("x2").PadLeft(2, '0') : "00")}";
                    ParaHelp.Bin_ADC[13].PValue = $"0x{(list.Count > 2 ? list[2].tag.ToString("x2").PadLeft(2, '0') : "00")}";
                    ParaHelp.Bin_ADC[14].PValue = $"0x{(list.Count > 3 ? list[3].tag.ToString("x2").PadLeft(2, '0') : "00")}";
                   
                }
                foreach (var item in ParaHelp.Bin_ADC)
                {
                    item.SaveMask = true;
                }
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！");
                return true;
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("保存错误！ 信息：" + ex.Message.ToString());
                return false;
            }
        }

    


        private void simpleButton7_Click(object sender, EventArgs e)
        {
            isSaveMask = SavePollingSetting();
            if (isSaveMask)//假如未保存
            {
                   this.Close();
            }
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void simpleButton8_Click(object sender, EventArgs e)
        {
            DialogResult dr = DevExpress.XtraEditors.XtraMessageBox.Show($"选择YES保存后退出！ \r\n 选择NO直接退出！", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            if (dr == DialogResult.No)
            {
                this.Close();
            }
            else
            {
                simpleButton7_Click(null, null);
            }
        }

        private void cmb_Polling_Pin_Select_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }
    }

    
}